package com.example.weight.householdWaste;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.greenSeasons.compost.duifeigongyi;
import com.example.weight.util.Utility;
import com.example.weight.util.WeightActivity;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class AbnormalRubbishActivity extends AppCompatActivity {

    private String name, factory;

    EditText carNum, rubbishSrc, rubbishKind, abnormalRubbishWeight, comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abnormal_rubbish);

        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        factory = previousIntent.getStringExtra("factory");

        TitleBar titleBar = findViewById(R.id.abnormal_rubbish_titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                AbnormalRubbishActivity.this.finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });

        carNum = findViewById(R.id.abnormal_rubbish_carNum);
        rubbishSrc = findViewById(R.id.abnormal_rubbish_rubbish_src);
        rubbishKind = findViewById(R.id.abnormal_rubbish_rubbish_kind);
        abnormalRubbishWeight = findViewById(R.id.abnormal_rubbish_weight);
        comment = findViewById(R.id.abnormal_rubbish_comment);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/shlj/abnormolGarbage/findForm" + "?factory=" + factory;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (!jsonObject.isNull("data")) {
                                new AlertDialog.Builder(AbnormalRubbishActivity.this)
                                        .setTitle("今日已填写此表")
                                        .setCancelable(false)
                                        .setPositiveButton("确定", null)
                                        .show();
                                JSONObject data = jsonObject.getJSONObject("data");
                                carNum.setText(data.getString("wagonNumber"));
                                rubbishSrc.setText(data.getString("garbageSource"));
                                rubbishKind.setText(data.getString("garbageType"));
//                                abnormalRubbishWeight.setText(data.getString("garbageNumber"));
//                                comment.setText(data.getString("remarks"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(AbnormalRubbishActivity.this,
                        "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }

    public void bulk_import(View view){

    }
    public void save(View view) {
        if (carNum.getText().toString().equals("")) {
            Toast.makeText(this, "请填写车号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (rubbishSrc.getText().toString().equals("")) {
            Toast.makeText(this, "请填写垃圾来源", Toast.LENGTH_SHORT).show();
            return;
        }
        if (rubbishKind.getText().toString().equals("")) {
            Toast.makeText(this, "请填写垃圾种类", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Utility.isNumeric(abnormalRubbishWeight.getText().toString())) {
            Toast.makeText(this, "垃圾重量需填写数字", Toast.LENGTH_SHORT).show();
            return;
        }
        new AlertDialog.Builder(this)
            .setTitle("确定保存吗？")
            .setCancelable(false)
            .setNegativeButton("取消", ((dialog, which) ->
                    Toast.makeText(this, "取消保存", Toast.LENGTH_SHORT).show()))
            .setPositiveButton("确定", (dialog, which) -> {

                RequestQueue queue = Volley.newRequestQueue(this);
                String url = Config.url + "/shlj/abnormolGarbage/addForm";

//                Map<String, String> map=new HashMap<>();
//                map.put("wagon_number", carNum.getText().toString());
//                map.put("garbage_source", rubbishSrc.getText().toString());
//                map.put("garbage_type", rubbishKind.getText().toString());
//                map.put("garbage_number", abnormalRubbishWeight.getText().toString());
//                map.put("remarks", comment.getText().toString());
//                JSONObject params=new JSONObject(map);
//                JsonObjectRequest objRequest = new JsonObjectRequest (Request.Method.POST, url, params,
//                        new Response.Listener<JSONObject>() {
//                            @Override
//                                public void onResponse(JSONObject obj) {
//                                Log.d("zys", obj.toString());
//                            }
//                        }, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
//                        Log.d("zys", error.getMessage());
//                    }
//
//                });

                StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Log.d("zys", response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    String code = jsonObject.getString("code");

//                                    carNum.setText("");
//                                    rubbishSrc.setText("");
//                                    rubbishKind.setText("");
//                                    abnormalRubbishWeight.setText("");
//                                    comment.setText("");

                                    if (code.startsWith("2"))
                                        Toast.makeText(AbnormalRubbishActivity.this,
                                                "保存成功", Toast.LENGTH_SHORT).show();
                                    else
                                        Toast.makeText(AbnormalRubbishActivity.this,
                                                "保存失败", Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(AbnormalRubbishActivity.this,
                                "保存失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> map=new HashMap<>();
                        map.put("wagon_number", carNum.getText().toString());
                        map.put("garbage_source", rubbishSrc.getText().toString());
                        map.put("garbage_type", rubbishKind.getText().toString());
                        map.put("garbage_number", abnormalRubbishWeight.getText().toString());
                        map.put("recording_person", name);
                        map.put("remarks", comment.getText().toString());
                        map.put("factory", factory);
                        return map;
                    }
                };
                stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                queue.add(stringRequest);

            }).show();
    }
}
